<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

use app\common\model\Transfer;
use app\common\model\UserAccountLog;
use think\Db;

/**
 * GamePlathform逻辑
 */
class Gameplathform extends LogicBase
{
    

    
    /**
     * 获取Recharge列表
     */
    public function getRechargeList($data = [],$state=0, $field = 'a.*,c.username ', $order = '', $paginate = DB_LIST_ROWS)
    {
        


        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =  $data['search_data'];
        }
        !empty($data['state']) &&    $where['a.state'] = $data['state'];

        !empty($data['type']) &&    $where['a.type'] = $data['type'];

        $where['a.status'] = 1;
        if (!empty($where1)) {

            $list =  $this->modelGamesLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->paginate($paginate, false, ['query' => $data]);
            $count =  $this->modelGamesLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->count();

        } else {

            $list =  $this->modelGamesLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->paginate($paginate, false, ['query' => $data]);
            $count =  $this->modelGamesLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->count();
        }





        return ['count'=>$count,'list'=>$list];

    }

    /**
     * 第三方申请转回记录导出
     */
    public function exportRechargelist($data, $field='a.*,c.username',$order = 'create_time desc')
    {

        $where = $where1 = [];
        if (!empty($data['date1']) && !empty($data['date2'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date1'] . ' 00:00:00'), strtotime($data['date2'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =  $data['search_data'];
        }


        !empty($data['state']) &&    $where['a.state'] = $data['state'];

        !empty($data['type']) &&    $where['a.type'] = $data['type'];

        $where['a.status'] = 1;
        if (!empty($where1)) {

            $list = $this->modelGamesLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->select();
        } else {

            $list =  $this->modelGamesLog->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();

        }
        foreach ($list as $k=>$v){
            $list[$k]['k']=$k+1;
            $list[$k]['state_text']=$v->state_text;
            $v->king_id>0 &&   $list[$k]['king_name']=$v->king->getData('name');
        }
        $action_name='';
        $excel_name='';
//        dd($list);
        switch ($data['type']){
            case 1:
                $titles = "序号,ID,用户名,转出金额,订单号,备注,状态,时间";
                $keys = "k,id,username,number,order_no,remark,state_text,create_time";
                $action_name='总包转游戏记录导出';
                $excel_name='总包转游戏记录';
                break;
            case 5:
                $titles = "序号,ID,赌王名称,用户名,转出金额,订单号,备注,状态,时间";
                $keys = "k,id,king_name,username,number,order_no,remark,state_text,create_time";
                $action_name='委托下注记录导出';
                $excel_name='委托下注记录';
                break;
            case 4:
                $titles = "序号,ID,用户名,下注金额,订单号,备注,状态,时间";
                $keys = "k,id,username,number,order_no,remark,state_text,create_time";
                $action_name='游戏下注记录导出';
                $excel_name='游戏下注记录';
                break;
        }
        action_log('导出', $action_name);

        export_excel($titles, $keys, $list, $excel_name);


    }



    /**


    /**
     * 充值审核
     */
    public function setStatus($data=[])
    {

        $info = $this->modelTransfer->getInfo(['id' => $data['id']]);

        Db::startTrans();
        try {

            $useracount = $this->modelUserAccount->where(['wallet_id' => $info['to_wallet_id'], 'user_id' => $info['user_id']])->lock(true)->find();
            if ($data['state'] == 1) {
                //审核通过

                //账户变动
                $number = $info['number'];

                $useracount->balance += $number;
                $useracount->save();
                $data['before_against_balance'] = $useracount->balance - $number;
                $data['after_against_balance'] = $useracount->balance;

                //写入流水
                UserAccountLog::create([
                    'user_id' => $info['user_id'],
                    'number' => $number,
                    'wallet_id' => $info['to_wallet_id'],
                    'before_blance' => $useracount->balance - $number,
                    'after_blance' => $useracount->balance,
                    'is_add' => 1,
                    'type' => 7
                ]);

            }
            $game=$this->modelGame->getInfo(['id'=>$info['game_id']]);
            //请求第三方接口
            if(!$game['domain_name2']){
                Db::rollback();
                return  [RESULT_ERROR, '该应用已关闭'];
            }

            $arr=[
                'serial_number'=>$info['serial_number'],
                'auth_account'=>session('member_info.username'),
                'account'=>data_md5_key($useracount->member->username),
                'token'=>'b1435fd198da820cf278186c0c8bdf5e'
            ];
            if($data['state']!=1){
                $arr['status']=1;
            }else{
                $arr['status']=0;
            }
          //  dd($arr);
            $url=$game['domain_name2'];
			//dump($arr);
            $result=https_curl($url,$arr,1);

            if(!$result){
                Db::rollback();
                return  [RESULT_ERROR, '该应用已关闭'];
            }

            $dat=json_decode($result,true);
            if($dat['code']==0){
                $data['admin_name']=  session('member_info.username');
                $data['game_trade_no']=  $dat['data']['withdrawal_no'];
                $result = $this->modelTransfer->setInfo($data);
            }else{
                Db::rollback();
                return  [RESULT_ERROR, $dat['msg']];
                return  [RESULT_ERROR, '该应用已关闭'];
            }
//            $data['admin_name']=  session('member_info.username');
//            $result = $this->modelTransfer->setInfo($data);
            $msg = $data['state'] == 1 ? '审核成功' : '拒绝审核';
            $result && action_log('充值审核', $msg);
            Db::commit();
            return $result ? [RESULT_SUCCESS, '审核成功'] : [RESULT_ERROR, '审核失败'];

        } catch (\Exception $e) {
            Db::rollback();
            return [RESULT_ERROR, $e->getMessage()];
        }
    }
    /**
     * 获取会员列表搜索条件
     */
    public function getWhere($data = [])
    {

        $where ['status']=1;




        return $where;
    }

    /**
     * 第三方申请转回记录导出
     */
    public function exportgamelist($data, $field='a.*,c.username',$order = 'create_time desc')
    {

        $where = $where1 = [];
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =  $data['username'];
        }
        if (!empty($data['wallet_id'])) {
            $where['a.to_wallet_id'] = $data['wallet_id'];
        }
        if (!empty($data['game_id'])) {
            $where['a.game_id'] = $data['game_id'];
        }else{
            $where['a.game_id'] =['gt',0];
        }
        $where['a.state'] = 0;
        $where['a.status'] = 1;
        $where['a.type'] = 7;
        if (!empty($where1)) {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->select();
        } else {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();

        }
        foreach ($list as $k=>$v){
          $list[$k]['k']=$k+1;
            $list[$k]['user_name']=$v->member->username;
            $list[$k]['wallet_name']=$v->towallet['name'];
            $list[$k]['game_name']=$v->game['name'];
            $list[$k]['add_time']=date('Y-m-d H:i:s',$v['create_time']);
        }
//        dd($list);
        $titles = "序号,ID,用户名,钱包类型,转回金额,应用名称,流水号,申请时间";
        $keys = "k,id,user_name,wallet_name,number,game_name,serial_number,add_time";

        action_log('导出', '导出第三方申请转回记录');

        export_excel($titles, $keys, $list, '第三方申请转回记录');
    }

    /**
     * 第三方转回已审核记录导出
     */
    public function exportGameCheckList($data, $field='a.*,c.username',$order = 'create_time desc')
    {

        $where = $where1 = [];
        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
        if (!empty($data['search_data'])) {
            $where1['a.user_id'] = $data['search_data'];
            $where1['c.username'] =  $data['username'];
        }
        if (!empty($data['wallet_id'])) {
            $where['a.to_wallet_id'] = $data['wallet_id'];
        }
        if (!empty($data['game_id'])) {
            $where['a.game_id'] = $data['game_id'];
        }else{
            $where['a.game_id'] =['gt',0];
        }
        $where['a.state'] = ['neq',0];
        $where['a.status'] = 1;
        $where['a.type'] = 7;
        if (!empty($where1)) {

            $list = $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->where(function ($q)use($where1){
                $q->whereOr($where1);
            })->order($order)->select();
        } else {

            $list =  $this->modelTransfer->alias('a')->join('__MEMBER__ c' ,'a.user_id = c.id')->field($field)->where($where)->order($order)->select();

        }
        foreach ($list as $k=>$v){
          $list[$k]['k']=$k+1;
                $list[$k]['user_name']=$v->member->username;
               $list[$k]['wallet_name']=$v->towallet['name'];
                 $list[$k]['game_name']=$v->game['name'];
           $list[$k]['state_text']=$v->state_text;
                 $list[$k]['add_time']=date('Y-m-d H:i:s',$v['create_time']);
                $list[$k]['update_time']=$v['update_time']?date('Y-m-d H:i:s',$v['update_time']):'';
        }
//        dd($list);
        $titles = "序号,ID,用户名,钱包类型,转回金额,应用名称,流水号,申请时间,状态,审核时间,审核人";
        $keys = "k,id,user_name,wallet_name,number,game_name,serial_number,add_time,state_text,update_time,admin_name";

        action_log('导出', '导出第三方转回已审核记录');

        export_excel($titles, $keys, $list, '第三方转回已审核记录');
    }





}
